<?php

namespace App\Services;

use App\Models\UserModel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class UserService extends BaseService
{
    /**
     * 用户列表
     *
     * @param int $page_size
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getPaginatedUsers(int $page_size): LengthAwarePaginator
    {
        $users = UserModel::query()
            ->select('id', 'name', 'role', 'created_at')
            ->where('deleted_at', 0)
            ->paginate($page_size ?: self::PAGE_SIZE);

        $userIds = $users->pluck('id')->toArray();

        $blacklistUsers = (new BlacklistService())->getBlackListByUserIds($userIds);
        $blacklistUserIds = $blacklistUsers->pluck('user_id')->toArray();

        foreach ($users as $user) {
            if (in_array($user->id, $blacklistUserIds)) {
                $user->setAttribute('is_blocked', 1);
            } else {
                $user->setAttribute('is_blocked', 2);
            }
        }

        return $users;
    }

    public function countUsers(): int
    {
        return UserModel::query()
            ->select('id')
            ->where('deleted_at', 0)
            ->count();
    }
}
